using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Acms.SqlDal;

namespace Acms.Bll
{
    public class TemplateFile : BaseItem
    {
        private TemplateFile() : base() { }
        private TemplateFile(Hashtable table) : base(table) { }
        private TemplateFile(Hashtable table, List<TemplateFile> collection) : base(table, collection) { }

        protected override Base CreateDal()
        {
            return new TemplateFileDal();
        }

        public static TemplateFile CreateInstance()
        {
            TemplateFile tmp = new TemplateFile();
            return tmp;
        }

        public static TemplateFile CreateInstance(int templateId, string name)
        {
            TemplateFile tmp = new TemplateFile();
            tmp.name = name;
            tmp.templateId = templateId;
            return tmp;
        }

        public static TemplateFile CreateInstance(Template template, string name)
        {
            TemplateFile tmp = new TemplateFile();
            tmp.name = name;
            tmp.templateId = template.Id;
            return tmp;
        }

        protected override void Load(Hashtable table)
        {
            id = (int)table[TemplateFileDal.Fields.Id];
            templateId = (int)table[TemplateFileDal.Fields.TemplateId];
            name = (string)table[TemplateFileDal.Fields.Name];
            path = (string)table[TemplateFileDal.Fields.Path];
            masterPagePath = (string)table[TemplateFileDal.Fields.MasterPagePath];
            theme = (string)table[TemplateFileDal.Fields.Theme];
            isDefault = (bool)table[TemplateFileDal.Fields.IsDefault];
        }

        private int id;
        public int Id
        {
            get { return id; }
        }

        private int templateId;
        public int TemplateId
        {
            get { return templateId; }
            set { templateId = value; }
        }

        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private string path;
        public string Path
        {
            get { return path; }
            set { path = value; }
        }

        private string masterPagePath;
        public string MasterPagePath
        {
            get { return masterPagePath; }
            set { masterPagePath = value; }
        }

        private string theme;
        public string Theme
        {
            get { return theme; }
            set { theme = value; }
        }


        private bool isDefault;
        public bool IsDefault
        {
            get { return isDefault; }
            set { isDefault = value; }
        }

        private static TemplateFile Get(Hashtable table)
        {
            if (table != null)
            {
                return new TemplateFile(table);
            }
            else
            {
                return null;
            }
        }

        private static List<TemplateFile> Get(Hashtable[] tables)
        {
            if (tables != null)
            {
                List<TemplateFile> tmp = new List<TemplateFile>();
                foreach (Hashtable table in tables)
                {
                    tmp.Add(new TemplateFile(table, tmp));
                }
                return tmp;
            }
            else
            {
                return null;
            }
        }

        public static TemplateFile GetById(int id)
        {
            TemplateFileDal dal = new TemplateFileDal();
            return Get(dal.GetById(id));
        }

        public static TemplateFile GetByTemplateIdAndName(int templateId, string name)
        {
            TemplateFileDal dal = new TemplateFileDal();
            return Get(dal.GetByTemplateIdAndName(templateId, name));
        }

        public static TemplateFile GetDefault(int templateId)
        {
            TemplateFileDal dal = new TemplateFileDal();
            return Get(dal.GetDefault(templateId));
        }

        public static List<TemplateFile> GetAllByTemplateId(int templateId)
        {
            TemplateFileDal dal = new TemplateFileDal();
            return Get(dal.GetAllByTemplateId(templateId));
        }

        public new void Update()
        {
            base.Update();
        }

        protected override object SaveChanges(ref object concurencyId, Base dal)
        {
            ((TemplateFileDal)dal).Update(ref id, templateId, name, path, masterPagePath, theme, isDefault, ref concurencyId);
            return id;
        }

        public new void Delete()
        {
            base.Delete();
        }

        protected override void DeleteRecord(object concurencyId, Base dal)
        {
            ((TemplateFileDal)dal).Delete(id, concurencyId);
        }
    }
}
